<template>
	<view>
		<view class="survey-top">{{surveyObj.activityName}}</view>
		<view class="survey-status">
			<text>{{'状态：'+status}}</text>
			<text>{{ surveyObj.answerStartTime +'至'+ surveyObj.answerEndTime }}</text>
		</view>
		<view class="survey-item">
			<view class="topic-tit">
				{{surveyItem.subjectname}}(<text>{{surveyItem.type}}</text>)
			</view>
			<view v-if="surveyItem.type==='单选题'" style="margin-left: 30rpx;">
				<uni-data-checkbox v-model="surveyItem.res" mode="list" :localdata="surveyItem.alternatives"
					:map="checkboxMap"></uni-data-checkbox>
			</view>
			<view v-if="surveyItem.type==='多选题'" style="margin-left: 30rpx;">
				<view>
					<uni-data-checkbox multiple v-model="surveyItem.res" mode="list"
						:localdata="surveyItem.alternatives" :map="checkboxMap"></uni-data-checkbox>
				</view>
			</view>
		</view>
		<view class="pagination">
			<button @click="serialNumber--" :disabled="serialNumber===1" size="mini">上一题</button>
			<view>当前第<text>{{serialNumber}}</text>题</view>
			<button @click="serialNumber++" :disabled="serialNumber===total" size="mini">下一题</button>
		</view>
		<button @click="subimt" v-if="serialNumber===total" class="subimt" type="success">提交</button>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="关闭" title="通知" :content="content" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="error" type="dialog">
			<uni-popup-dialog type="error" cancelText="关闭" title="通知" content="请做完所有题目再提交！"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="popup" background-color="#fff">
			<view class="popup-content">
				<view class="popup-item">
					<text>昵称</text>
					<input class="nickname" type="nickname" :value="member.memberName" @blur="bindblur"
						placeholder="点击获取微信昵称" />
				</view>
			</view>
			<button type="success" @click="close">关闭</button>
		</uni-popup>
	</view>
</template>

<script>
	import * as survey from '../../common/api/survey.js'
	export default {
		data() {
			return {
				uid:'',
				surveyObj: {},
				surveyList: [],
				status: '',
				total: 0,
				serialNumber: 1,
				checkboxMap: {
					text: 'value',
					value: 'options'
				},
				score: 0,
				msgType: '',
				content: '',
				member: {
					memberName:''
				}
			};
		},
		onLoad(e) {
			if(!uni.getStorageSync('memberName') || !uni.getStorageSync('avatarUrl')){
				uni.reLaunch({
					url: `/pages/mine/login?url=/pages/index/index`
				});
			}
			this.uid = e.uid
			this.getData(this.uid)
			this.getResult();
		},
		onShow() {
			// this.member.memberId = uni.getStorageSync('openid_key')
			// if (uni.getStorageSync('memberName')) {
			// 	this.member.memberName = uni.getStorageSync('memberName')
			// 	this.getResult()
			// } else {
			// 	this.$refs.popup.open('top')
			// }
		},
		computed: {
			surveyItem() {
				return this.surveyList[this.serialNumber - 1]
			}
		},
		methods: {
			getData(uid) {
				const param = {
					uid: uid
				}
				survey.querySurvey(param).then(res => {
					this.surveyObj = res.data.data.rows[0]
					if (new Date(this.surveyObj.answerStartTime).getTime() > new Date().getTime()) {
						this.status = '未开始'
					} else if (new Date(this.surveyObj.answerEndTime).getTime() > new Date().getTime()) {
						this.status = '调查中'
					} else if (new Date(this.surveyObj.answerEndTime).getTime() < new Date().getTime()) {
						this.status = '已结束'
					}
					this.surveyList = this.surveyObj['sysActivityQuestionRelationDO']
					this.surveyList.map(item => {
						item.alternatives = JSON.parse(item.alternatives)
						if (item.type === '单选题') {
							this.$set(item, 'res', '')
						} else {
							this.$set(item, 'res', [])
						}
						if(item.alternatives != null){
							item.alternatives.map(item => {
								item.value = item.options + ':' + item.value
							})
						}
					})
					this.total = this.surveyList.length
				}).catch(error => {
					console.log(error)
				})
			},
			subimt() {
				this.score = 0
				let submitList = []
				try {
					this.surveyList.forEach(item => {
						if (item.type === '单选题') {
							if (item.res === item.result) {
								this.score = this.score + Number(item.score)
							}
							submitList.push({
								sysActivityUid: this.surveyObj.uid,
								sysActivityName: this.surveyObj.activityName,
								sysQuestionUid: item.uid,
								subjectName: item.subjectname,
								classify: this.surveyObj.classify,
								answer: item.res,
								results: item.result,
								type: item.type,
								score: 0,
								standardScore: item.score,
								memberId: this.member.memberId,
								memberName: this.member.memberName
							})
						} else if (item.type === '多选题') {
							if (JSON.stringify(item.res) === item.result) {
								this.score = this.score + Number(item.score)
							}
							submitList.push({
								sysActivityUid: this.surveyObj.uid,
								sysActivityName: this.surveyObj.activityName,
								sysQuestionUid: item.uid,
								subjectName: item.subjectname,
								classify: this.surveyObj.classify,
								answer: JSON.stringify(item.res),
								results: item.result,
								type: item.type,
								score: 0,
								standardScore: item.score,
								memberId: this.member.memberId,
								memberName: this.member.memberName
							})
						}
					})
				} catch (err) {
					this.$refs.error.open()
					return false
				}
				survey.insertBatch(submitList).then(res => {
					if (res.data.code == 200) {
						this.msgType = 'success'
						this.content = '感谢参与，您的分数是' + this.score + '分。'
						this.$refs.alertDialog.open()
					}
				}).catch(error => {
					console.error(error);
				});
			},
			dialogConfirm() {
				uni.navigateBack({
					delta: 1
				});
			},
			dialogClose() {
				uni.navigateBack({
					delta: 1
				});
			},
			bindblur(e) {
				this.member.memberName = e.detail.value
				//将更改的昵称上传给接口
			},
			close() {
				uni.setStorageSync('memberName', this.member.memberName)
				this.getResult()
				this.$refs.popup.close();
			},
			getResult() {
				let param = {
					sysActivityUid: this.uid,
					memberId: this.member.memberId
				}
				survey.queryByPage(param).then(res => {
					if (res.data.code == 200) {
						if (res.data.data.rows == null || res.data.data.rows.length > 0) {
							this.msgType = 'success',
							this.content = '本次活动结束！'
							this.$refs.alertDialog.open()
						}
					}
				}).catch(error => {
					console.error(error);
				});
			}
		},

	}
</script>

<style scoped lang="scss">
	.survey-top {
		padding: 20rpx;
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
	}

	.survey-status {
		display: flex;
		justify-content: space-between;
		padding: 20rpx;
		font-size: 24rpx;
		color: #666;
	}

	.survey-item {
		margin: 20rpx;
		width: 710rpx;

		.topic-tit {
			font-size: 28rpx;
			font-weight: bold;
		}
	}

	.pagination {
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #666;
	}

	.subimt {
		margin: 50rpx 150rpx;
		background: #00aa7f;
		color: #fff;
	}
</style>